package com.dbms.controller;

import com.dbms.common.annotation.ResponseResult;
import com.dbms.domain.dto.DownloadExcelDTO;
import com.dbms.domain.entity.FloodmapSite;
import com.dbms.domain.entity.FloodmapStationPropagate;
import com.dbms.domain.vo.BisXajmodelArearateVO;
import com.dbms.domain.vo.DataObject;
import com.dbms.service.BisXajmodelArearateService;
import com.dbms.service.FloodmapSiteService;
import com.dbms.service.FloodmapStationPropagateService;
import com.dbms.service.GenericExcelImportService;
import com.dbms.utils.ExcelGenerator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

@Tag(name = "excel操作", description = "excel操作")
@RestController
@RequestMapping("/api/excel")
@RequiredArgsConstructor
public class GenericExcelController {

    final GenericExcelImportService excelImportService;
    final BisXajmodelArearateService bisXajmodelArearateService;
    final FloodmapSiteService floodmapSiteService;
    final FloodmapStationPropagateService floodmapStationPropagateService;

    @Operation(summary = "导入excel", description = "导入excel")
    @PostMapping("/import")
    public ResponseResult<List<DataObject>> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        return ResponseResult.success(excelImportService.importExcel(file));
    }

    @Operation(summary = "下载excel模版", description = "下载excel模版")
    @PostMapping("/download-excel")
    public ResponseEntity<byte[]> downloadExcel(@RequestBody DownloadExcelDTO dto) throws IOException {
        FloodmapSite site = floodmapSiteService.getById(dto.getSiteId());
        FloodmapStationPropagate propagate = floodmapStationPropagateService.getByStationCod(dto.getStcdId());
        List<BisXajmodelArearateVO> list = bisXajmodelArearateService.queryByStcdRToList(dto.getStcdId());

        // 表头字段名列表
        List<String> headers = new ArrayList<>();
        headers.add("日期");

        for (BisXajmodelArearateVO vo : list) {
            headers.add(vo.getStcdP());
        }

        ByteArrayOutputStream outputStream = ExcelGenerator.generateExcel(null, headers);

        HttpHeaders headersResponse = new HttpHeaders();
        headersResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        String name = site.getName()+propagate.getStationName();
        String encodedFileName = URLEncoder.encode(name + "模版.xlsx", StandardCharsets.UTF_8.toString());
        headersResponse.setContentDispositionFormData("attachment", encodedFileName);

//        headersResponse.setContentDispositionFormData("attachment", System.currentTimeMillis()+".xlsx");

        return ResponseEntity.ok()
                .headers(headersResponse)
                .body(outputStream.toByteArray());
    }
}
